package cd.edu.xmist.tank;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class EnemyTank implements AbstractTank{

    private BufferedImage imageU;
    private BufferedImage imageD;
    private BufferedImage imageL;
    private BufferedImage imageR;

    private Frame frame;
    /**
     * 移动的状态
     */

    private Boolean moving = false;
    /**
     * 炮管指向的方向
     */
    private Direction direction = Direction.UP;


    /**
     * 图片的缓冲
     * @param frame
     */
    private BufferedImage image;

    /**
     *
     * @param fame
     */
    private int x = 0;
    private int y = 65;


    private int speed =13;

    public EnemyTank(Frame fame) {
        try {
            this.imageU = ImageIO.read(EnemyTank.class.getClassLoader().getResourceAsStream("./img/enemy1U.gif"));
            this.imageD = ImageIO.read(EnemyTank.class.getClassLoader().getResourceAsStream("./img/enemy1D.gif"));
            this.imageL = ImageIO.read(EnemyTank.class.getClassLoader().getResourceAsStream("./img/enemy1L.gif"));
            this.imageR = ImageIO.read(EnemyTank.class.getClassLoader().getResourceAsStream("./img/enemy1R.gif"));
        } catch (IOException e) {
            System.out.println("游戏加载图片失败");
            e.printStackTrace();
        }
   /**
    * 游戏开始时，初始方向
   */
       this.image = this.imageU;
       this.frame = fame;
    }

    @Override
    public void startMoveLeft() {
        this.moving = true;
        this.image = this.imageL;
        this.direction = Direction.LEFT;
    }

    @Override
    public void startMoveRight() {
        this.moving = true;
        this.image = this.imageR;
        this.direction = Direction.RIGHT;
    }

    @Override
    public void startMoveUp() {

        this.moving = true;
        this.image = this.imageU;
        this.direction = Direction.UP;
    }

    @Override
    public void startMoveDown() {
        this.moving = true;
        this.image = this.imageD;
        this.direction = Direction.DOWN;
    }

    @Override
    public void stopMoveLeft() {
        this.moving = false;

    }

    @Override
    public void stopMoveRight() {
        this.moving = false;
    }

    @Override
    public void stopMoveUp() {
        this.moving = false;
    }

    @Override
    public void stopMoveDown() {
        this.moving = false;
    }

    @Override
    public int getX() {
        return 0;
    }

    @Override
    public int getY() {
        return 0;
    }

    @Override
    public void fire() {

    }

    @Override
    public void paint(Graphics g) {
        /**
         * 1.计算坐标
         */
        /**
         * 表示坦克正在移动
         */
        if(this.moving){
            switch (this.direction){
                case UP:
                    this.y -=this.speed;
                    break;
                case DOWN:
                    this.y +=this.speed;
                    break;
                case LEFT:
                    this.x -=this.speed;
                    break;
                case RIGHT:
                    this.x +=this.speed;
                    break;
            }
        }
        /**
         * 判定坦克不能超出界面（注意点；坦克自身有尺寸，60*60）
         */
        /**
         * 右边界
         */

            if(this.x >= this.frame.getWidth() - 60){
                this.x = this.frame.getWidth() - 60;
            }

        /**
         * 左边界
         */
        if(this.x < 7){
            this.x = 7;
        }

        /**
         * 上边界
         */
        if(this.y < 30){
            this.y = 30;
        }
        /**
         * 下边界
         */
        if(this.y >= this.frame.getHeight() -60){
            this.y = this.frame.getHeight() -60;
        }

        /**
         * 2.根据坦克坐标画制
         */
        g.drawImage(this.image,this.x,this.y,null);
    }
}
